package com.gjy.feature.login;

import com.gjy.encryption.digest.DigestCoder;

import java.nio.charset.StandardCharsets;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-11-10 14:28:00
 */
public class UsernamePasswordLogin implements Login<User> {

    @Override
    public boolean login(User user) {
        if (!user.getActive()) {
            return false;
        }
        String pwd = pwd(user.getPassword(), user.getSalt(), user.getSize());
        System.out.println("pwd = " + pwd);
        return true;
    }

    private String pwd(String password, String salt, Integer size) {
        DigestCoder coder = new DigestCoder();
        byte[] digest = coder.encryptDigest(password.getBytes(StandardCharsets.UTF_8),
                salt.getBytes(StandardCharsets.UTF_8), size, DigestCoder.SHA.SHA_512);
        return new String(digest, StandardCharsets.UTF_8);
    }
}
